package com.cz.second.kill.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * <p>redis工具类</p>
 * <p>
 * <pre>
 * @author gitamacai
 * @date 2019/12/5 17:16
 * </pre>
 */
@Component
public final class RedisUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void rPush(String redisKey, String value) {
        redisTemplate.execute((RedisCallback<Void>) redisConnection -> {
            redisConnection.rPush(redisKey.getBytes(), value.getBytes());
            return null;
        });
    }

    public String lpop(String redisKey) {
        return redisTemplate.execute((RedisCallback<String>) redisConnection -> {
            byte[] result = redisConnection.lPop(redisKey.getBytes());
            return result == null ? "" : new String(result);
        });
    }
}